package com.eshin.atuoconfig;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.eshin.validatetor.Validator;


@Configuration  //相当于配置xml中的beans配置
@EnableConfigurationProperties(HelloProperties.class)//开启属性注入,通过@autowired注入  
@ConditionalOnClass(HelloWorld.class)//判断这个类是否在classpath中存在  
//当设置hello=enabled的情况下,如果没有设置则默认为true,即条件符合  
@ConditionalOnProperty(prefix="hello", value="enabled", matchIfMissing = true)  
public class HelloAutoConfiguration {  
      
    @Resource(name="helloProperties") 
    private HelloProperties helloProperties;  
      
    @Bean//使用java配置方式配置这个类  相当于配置xml中的bean配置
    @ConditionalOnMissingBean(HelloWorld.class)//容器中如果没有Hello这个类,那么自动配置这个Hello  
    public HelloWorld getHelloWorld() { 
//    	变量名即为配置在spring容器的bean名
    	HelloWorld helloWorld = new HelloWorld();  
    	helloWorld.setMsg(helloProperties.getMsg());  
        return helloWorld;  
    }  
      
    @Bean
    public ITestBean getFTestBean(){
    	return new FTestBean();
    }
    @Bean
    public ITestBean getSTestBean(){
    	return new STestBean();
    }
    
    
    @Autowired(required=false)
    List<ITestBean>testBeans = Collections.emptyList();
    
    @Autowired(required=false)
    Map<String,ITestBean>testBeansMap = Collections.emptyMap();
    
    @Autowired(required=false)
    List<Validator<?>>validator = Collections.emptyList();
    /**
     * 此处将上面的两个bean注解到下面的List<ITestBean> testBeans中作为参数
     * @param testBeans
     * @return
     */
    @Bean
    public TestBeanContainer getbeans(List<ITestBean> testBeans){
    	System.out.println(testBeans.size());
    	System.out.println(this.testBeans.size());
    	testBeans.forEach(new Consumer<ITestBean>() {

			@Override
			public void accept(ITestBean t) {
				System.out.println(t.getId());
			}
		});
    	return new TestBeanContainer(testBeans);
    }
} 